// pages/list/list.js
import {baseApi} from '../../../utils/util.js';
Page({
  data: {
    openid:'',
    items: [],      // 列表数据
    page: 1,        // 当前页码
    pageSize: 10,   // 每页数量
    hasMore: true,  // 是否还有更多数据
    loading: false  // 是否正在加载
  },

  onLoad() {
    var that = this;
    wx.getStorage({
      key: 'openid',
      success (res) {
        console.log(JSON.stringify(res));
        that.data.openid = res.data;
        console.log(res.data);
        that.loadData();
      }
    })
  },

  // 加载数据
  async loadData() {
    if (!this.data.hasMore || this.data.loading) return
    
    this.setData({ loading: true })
    var that = this;
    try {
      // 模拟API请求
      // const res = await new Promise(resolve => {
      //   setTimeout(() => {
      //     resolve({
      //       data: Array.from({ length: this.data.pageSize }, (_, i) => ({
      //         id: this.data.page * this.data.pageSize + i + 1,
      //         state: '[审批中]',
      //         title: `2025-05-01 09:11 访问 ${this.data.page * this.data.pageSize + i + 1}`,
      //         content: `这是第 ${this.data.page * this.data.pageSize + i + 1} 访问事由 `
      //       })),
      //       hasMore: this.data.page < 5 // 模拟最多5页数据
      //     })
      //   }, 500)
      // })
      var params = {
        pageNum: that.data.page,
        pageSize: that.data.pageSize,
        openid: that.data.openid
      };
      console.log(JSON.stringify(params));

      wx.request({
        url:  baseApi() + '/app/myAppointmentList',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 关键请求头
        },
        data: params,
        success(res){
          console.log(JSON.stringify(res));
          that.setData({
            items: that.data.items.concat(res.data.rows),
            page: that.data.page + 1,
            hasMore: that.data.page < 5 && res.data.rows.length == that.data.pageSize,
            loading: false
          });
        }
      })
      
    } catch (error) {
      console.error('加载失败:', error)
      this.setData({ loading: false })
    }
  },

  // 点击列表项
  handleItemTap(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=${id}'
    })
  },

  // 滑动结束处理
  handleSwipeEnd(e) {
    const x = e.detail.x
    const index = e.currentTarget.dataset.index
    const query = wx.createSelectorQuery().in(this)
    
    // 计算滑动阈值
    query.select('.swipe-item').boundingClientRect(rect => {
      const threshold = -rect.width * 0.3
      if (x < threshold) {
        this.setData({
          [`items[${index}].showDelete`]: true
        })
      } else {
        this.setData({
          [`items[${index}].showDelete`]: false
        })
      }
    }).exec()
  },

  // 删除项目
  deleteItem(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      items: this.data.items.filter(item => item.id !== id)
    })
    wx.showToast({ title: '删除成功' })
  },
  // 亮码
  viewCode(e){
    var id = e.currentTarget.dataset.id;
    var approvalStatus = e.currentTarget.dataset.status;
    console.log(approvalStatus);
    if(approvalStatus != 'A'){
      wx.showToast({
        title: '审批未通过,无法亮码',
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/visitor/view_code/index?id='+id,
    })
  },
  // 加载更多
  loadMore() {
    this.loadData()
  },
  detailItem(e){
    var id = e.currentTarget.dataset.id;
    var approvalStatus = e.currentTarget.dataset.status;
    console.log(approvalStatus);
    wx.navigateTo({
      url: '/pages/visitor/detail/index?id='+id,
    })
  }
})